package sdc.com.petscalendar.fragments;

import java.util.ArrayList;
import java.util.Random;

import sdc.com.petscalendar.R;
import sdc.com.petscalendar.adapters.ParasiteAdapter;
import sdc.com.petscalendar.objects.Parasite;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ParasiteListFragment extends BaseFragment implements
		View.OnClickListener {

	private RadioGroup mRgrType;
	private View mTabDog, mTabCat;
	private ListView mListView;
	private ArrayList<Parasite> parasites;
	private ParasiteAdapter mAdapter;
	private static boolean isTabDogSelected = true;
	private static int type = R.id.rb_all;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_parasite_list,
					container, false);
		}
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initComponents();
		addListeners();
		if (isTabDogSelected)
			mTabDog.performClick();
		else
			mTabCat.performClick();
		((RadioButton) getView().findViewById(type)).setChecked(true);
	}

	private void initComponents() {
		mTabDog = getView().findViewById(R.id.tab_dog);
		mTabCat = getView().findViewById(R.id.tab_cat);

		mRgrType = (RadioGroup) getView().findViewById(R.id.rgr_parasite_type);

		mListView = (ListView) getView().findViewById(R.id.list);
		mAdapter = new ParasiteAdapter(getActivity(), parasites);
		mListView.setAdapter(mAdapter);

	}

	private void addListeners() {
		mTabCat.setOnClickListener(this);
		mTabDog.setOnClickListener(this);
		mAdapter.setItemClickListener(new ParasiteAdapter.ItemClickListener() {

			@Override
			public void onItemClick(int position) {
				Parasite parasite = mAdapter.getItem(position);

				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				ParasiteDetailFragment fragment = new ParasiteDetailFragment();
				fragment.setParasite(parasite);
				fragmentTransaction.replace(R.id.frm_parasite, fragment);
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		mRgrType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				type = checkedId;
				switch (checkedId) {
				case R.id.rb_all:
					mAdapter.filter(Parasite.ALL);
					mAdapter.notifyDataSetChanged();
					break;
				case R.id.rb_internal:
					mAdapter.filter(Parasite.INTERNAL);
					mAdapter.notifyDataSetChanged();
					break;
				case R.id.rb_external:
					mAdapter.filter(Parasite.EXTERNAL);
					mAdapter.notifyDataSetChanged();
					break;
				default:
					break;
				}

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab_dog:
			if (!mTabDog.isSelected()) {
				mTabDog.setSelected(true);
				mTabCat.setSelected(false);
			}
			break;
		case R.id.tab_cat:
			if (!mTabCat.isSelected()) {
				mTabDog.setSelected(false);
				mTabCat.setSelected(true);
			}
			break;
		default:
			break;
		}

	}

	private void initData() {
		parasites = new ArrayList<Parasite>();
		for (int i = 0; i < 10; i++) {
			Random random = new Random();
			Parasite parasite = new Parasite();
			parasite.setId(i);
			parasite.setAvatar("");
			parasite.setName("Puces " + i);
			parasite.setDescription("Most lice are scavengers, feeding on skin and other debris found on the host's body, but some species feed on sebaceous secretions and blood. Most are found only on specific types of animals, and, in some cases, only to a particular part of the body; some animals are known to host up to fifteen different species, although one to three is typical for mammals, and two to six for birds. For example, in humans, different species of louse inhabit the scalp and pubic hair. Lice generally cannot survive for long if removed from their host. A louse's color varies from pale beige to dark gray; however, if feeding on blood, it may become considerably darker. Female lice are usually more common than the males, and some species are even known to be parthenogenetic. A louse's egg is commonly called a nit. Many lice attach their eggs to their host's hair with specialized saliva; the saliva/hair bond is very difficult to sever without specialized products. Lice inhabiting birds, however, may simply leave their eggs in parts of the body inaccessible to preening, such as the interior of feather shafts. Living lice eggs tend to be pale white. Dead lice eggs are more yellow.Lice are exopterygotes, being born as miniature versions of the adult, known as nymphs. The young moult three times before reaching the final adult form, usually within a month of hatching.");

			boolean rdb = random.nextBoolean();
			parasite.setType(rdb ? Parasite.INTERNAL : Parasite.EXTERNAL);

			parasites.add(parasite);
		}
	}
}
